﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_Common;
using Nova_MOD;
using Nova_BLL;
using Nova_DAL;
using System.Data.SqlClient;
using System.Data.Common;

public partial class Views_CargoStatus : System.Web.UI.Page
{
    string lbl_LaneNo = ""; //巷道号
    string cid = "";        ////层
    int rid = 0;        //gridview行的编号
    int iLcol = 0;
    string PosID = "";


    DataSet ds = null;
    DataTable myTable = null;
    int PosState = 0;           //货位状态
    int iLineNo = 0;               //排
    int iLayNo = 0;           //层
    string PID = "";
    DbHelper access = new DbHelper();


    Nova_Common.LoginLogic cLoginLogic = new LoginLogic();
    Nova_BLL.TBase_Position Nova_BLL_TBase_Position = new Nova_BLL.TBase_Position();
    Nova_BLL.TLaneDefine Nova_BLL_TLaneDefine = new Nova_BLL.TLaneDefine();
    Nova_BLL.TPosState Nova_BLL_TPostState = new Nova_BLL.TPosState();
    Nova_MOD.TBase_Position Nova_MOD_TBase_Position = new Nova_MOD.TBase_Position();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Nova_Common.LoginLogic.IsLogin();
            ddlbind();
            dlistPosbind();
            gvbind();

        }
    }


    protected void ddlbind()
    {
        ds = Nova_BLL_TLaneDefine.DataSetTLaneDefine();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            ddlNo_Lane.DataSource = ds;
            ddlNo_Lane.DataValueField = "No_Lane";
            ddlNo_Lane.DataTextField = "No_Lane_Text";
            ddlNo_Lane.DataBind();
        }
    }
    protected bool reInt(string strNum)
    {
        try
        {
            int var1 = Convert.ToInt32(strNum);
            return true;
        }
        catch
        {
            return false;
        }
    }

    protected void btn_Click(object sender, EventArgs e)
    {

        Button btn = (Button)sender;
        if (!string.IsNullOrEmpty(btn.CommandArgument.Trim()))
        {
            int ibtn = int.Parse(btn.CommandArgument);//修改的货位状态值
            
            if (txtELcol.Text == "" || txtELine.Text == "" || txtSLcol.Text == "" || txtSLine.Text == "")
            {
                //ShowBox.MessageBox(this.Page, "请选择货位号！");
                lblMsg.Text = "请选择货位号！";
                return;
            }
            else if (!reInt(txtSLine.Text) || !reInt(txtELine.Text) || !reInt(txtSLcol.Text) || !reInt(txtELcol.Text))
            {
                //ShowBox.MessageBox(this.Page, "请选择货位号！");
                lblMsg.Text = "坐标请输入数字！";
                return;
            }
            else
            {
                for (int i = Convert.ToInt32(txtSLine.Text); i <= Convert.ToInt32(txtELine.Text); i++)
                {
                    for (int j = Convert.ToInt32(txtSLcol.Text); j <= Convert.ToInt32(txtELcol.Text); j++)
                    {
                        PosState = Convert.ToInt32(Nova_BLL_TPostState.intPosID(GV.Rows[i - 1].Cells[j].Text.ToString()));       //取货位状态
                        lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
                        iLineNo = Convert.ToInt32(GV.Rows[i - 1].Cells[0].Text.Substring(0, 1));     //排
                        iLcol = j;         //列
                        string strLayNo=GV.Rows[i - 1].Cells[1].Text;
                        iLayNo = Convert.ToInt32(strLayNo.Substring(0,strLayNo.Length-1));     //层

                        Nova_MOD_TBase_Position.No_Lane = int.Parse(lbl_LaneNo);
                        Nova_MOD_TBase_Position.No_Line = iLineNo;
                        Nova_MOD_TBase_Position.No_Col = iLcol;
                        Nova_MOD_TBase_Position.No_Layer = iLayNo;
                        PID = Nova_BLL_TBase_Position.GetiPID(Nova_MOD_TBase_Position);
                        string strMessage = Nova_BLL_TBase_Position.UpdatePosition(PID, ibtn);

                        if (strMessage != "")
                        {
                            lblMsg.Text = strMessage;
                        }
                    }
                }
            }
            txtELcol.Text = "";
            txtELine.Text = "";
            txtSLcol.Text = "";
            txtSLine.Text = "";
            gvbind();
        }
    }

    protected void gvbind()
    {
        lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();
        myTable = Nova_BLL_TBase_Position.DTableTBase_Position(lbl_LaneNo);
        if (myTable != null && myTable.Rows.Count > 0)
        {
            GV.DataSource = myTable;
            GV.DataBind();
        }
    }

    protected void ddlNo_Lane_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvbind();
    }



    protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes["style"] = "width:30px;";
            cid = e.Row.Cells[1].Text.ToString().Substring(0, 1);
            rid = e.Row.RowIndex + 1;
            lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
            iLineNo = Convert.ToInt32(e.Row.Cells[0].Text.Substring(0, 1));     //排
            //iLcol = e.Row.RowIndex;         //列
            iLayNo = Convert.ToInt32(e.Row.Cells[1].Text.Substring(0, 1));     //层
            DataRowView dv = (DataRowView)e.Row.DataItem;
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                //列从2开始
                //if (i == 0)
                //{
                //    iLcol = 2;
                //}
                //else if (i == 1)
                //{
                //    iLcol = 2;
                //}
                //else
                //{
                //    iLcol = i;
                //}

                //列从1开始
                if (i == 0)
                {
                    iLcol = 1;
                }
                else if (i == 1)
                {
                    iLcol = 1;
                }
                else
                {
                    iLcol = i - 1;
                }
                if (i > 1)
                {
                    if (e.Row.Cells[i].Text.ToString() == "1")
                    {
                        PosID = "10" + lbl_LaneNo.PadLeft(2, '0') + iLineNo.ToString() + iLcol.ToString().PadLeft(3, '0') + iLayNo.ToString().PadLeft(2, '0') + "00";
                        //e.Row.Cells[i].Attributes["ondblclick"] = string.Format("window.showModalDialog('StorageList.aspx?PosID={0}', window, 'dialogWidth=900px;dialogHeight=450px;scroll:no;resizable:no;status:0;');",PosID);
                    }
                    e.Row.Cells[i].Text = Nova_BLL_TPostState.strPosImg(e.Row.Cells[i].Text.ToString());

                }



                e.Row.Cells[i].Attributes["onclick"] = "showEdit(" + lbl_LaneNo + "," + e.Row.Cells[0].Text.ToString().Substring(0, 1) + "," + iLcol + "," + cid + "," + rid + ");";


            }
        }
    }

    protected void dlistPosbind()
    {
        ds = Nova_BLL_TPostState.DataListPosState();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            dlistPos.DataSource = ds;
            dlistPos.DataBind();
        }
    }

    protected void ibtnSelect_Click(object sender, ImageClickEventArgs e)
    {
        //开始的坐标不能为空，结束的坐标为空，只处理开始坐标的那一个单元格
        if (txtSLine.Text != "" && txtSLcol.Text != "")
        {
            if (!reInt(txtSLine.Text))
            {
                //ShowBox.MessageBox(this.Page, "开始坐标请输入数字!");
                lblMsg.Text = "开始坐标请输入数字!";
                return;
            }
            if (!reInt(txtSLcol.Text))
            {
                //ShowBox.MessageBox(this.Page, "开始坐标请输入数字!");
                lblMsg.Text = "开始坐标请输入数字!";
                return;
            }
            if (txtELine.Text != "" && txtELine.Text != "")
            {
                if (!reInt(txtELine.Text))
                {
                    //ShowBox.MessageBox(this.Page, "结束坐标请输入数字!");
                    lblMsg.Text = "结束坐标请输入数字!";
                    return;
                }
                if (!reInt(txtELcol.Text))
                {
                    //ShowBox.MessageBox(this.Page, "结束坐标请输入数字!");
                    lblMsg.Text = "结束坐标请输入数字!";
                    return;
                }

                int SLine = Convert.ToInt32(txtSLine.Text.ToString());
                int ELine = Convert.ToInt32(txtELine.Text.ToString());
                int iLine = 0;

                int SLcol = Convert.ToInt32(txtSLcol.Text.ToString());
                int ELcol = Convert.ToInt32(txtELcol.Text.ToString());
                int iLcol = 0;

                if (SLine > ELine)
                {
                    txtSLine.Text = ELine.ToString();
                    txtELine.Text = SLine.ToString();
                    iLine = SLine;
                    SLine = ELine;
                    ELine = iLine;

                }

                if (SLcol > ELcol)
                {
                    txtSLcol.Text = ELcol.ToString();
                    txtELcol.Text = SLcol.ToString();
                    iLcol = SLcol;
                    SLcol = ELcol;
                    ELcol = iLcol;
                }

                
                for (int x = SLine; x <= ELine; x++)
                {
                    for (int j = SLcol; j <= ELcol; j++)
                    {
                        GV.Rows[x - 1].Cells[j + 1].BackColor = System.Drawing.Color.FromName("#ff0000"); //列从1开始
                        //GV.Rows[x - 1].Cells[j].BackColor = System.Drawing.Color.FromName("#ff0000");   //列从2开始
                    }
                }
            }
            else
            {
                GV.Rows[Convert.ToInt32(txtSLine.Text) - 1].Cells[Convert.ToInt32(txtSLcol.Text) + 1].BackColor = System.Drawing.Color.FromName("#ff0000"); //列从1开始
                //GV.Rows[Convert.ToInt32(txtSLine.Text) - 1].Cells[Convert.ToInt32(txtSLcol.Text)].BackColor = System.Drawing.Color.FromName("#ff0000");   //列从2开始
               
            }
        }
    }
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtELcol.Text = "";
        txtELine.Text = "";
        txtSLcol.Text = "";
        txtSLine.Text = "";
        lblMsg.Text = "";
        gvbind();
    }




    //protected void btnzhupan_Click(object sender, EventArgs e)
    //{
    //    int iResNo = 0;
    //    string strResMsg = "";

    //    if (txtPos_ID.Text == "0" || txtPos_ID.Text == "")
    //    {
    //        ShowBox.MessageBox(this.Page, "请填写货位号！");
    //    }
    //    else
    //    {
    //        cmd = access.GetStoredProcCommond("SP_ASRS_Generate_Out_CMDS_ByPos");
    //        access.AddInParameter(cmd, "@PosS_ID", DbType.String, txtPos_ID.Text.ToString().Trim());
    //        access.AddInParameter(cmd, "@SQIDE", DbType.String, 0);
    //        access.AddInParameter(cmd, "@USERNAME", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
    //        access.AddOutParameter(cmd, "@RESNO", DbType.Int32, 100);
    //        access.AddOutParameter(cmd, "@RESMSG", DbType.String, 255);
    //        access.ExecuteNonQuery(cmd);

    //        iResNo = Convert.ToInt32(access.GetParameter(cmd, "@RESNO").Value.ToString());
    //        strResMsg = access.GetParameter(cmd, "@RESMSG").Value.ToString();
    //        if (iResNo > 0)
    //        {
    //            Label1.Text = "整出成功！";
    //        }
    //        else
    //        {
    //            Label1.Text = "整出失败！";
    //            Label1.Text += strResMsg;
    //        }
    //    }
    //}

    //protected void btnpandian_Click(object sender, EventArgs e)
    //{
    //    int iResNo = 0;

    //    cmd = access.GetStoredProcCommond("SP_ASRS_Generate_PanDian_CMDS_ByPos");
    //    access.AddInParameter(cmd, "@PosS_ID", DbType.String, txtPos_ID.Text.ToString().Trim());
    //    access.AddInParameter(cmd, "@USERNAME", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
    //    access.AddOutParameter(cmd, "@RESNO", DbType.Int32, 100);
    //    access.AddOutParameter(cmd, "@RESMSG", DbType.String, 255);
    //    iResNo = access.ExecuteNonQuery(cmd);
    //    if (iResNo > 0)
    //    {
    //        Label1.Text = "盘点成功！";
    //    }
    //    else
    //    {
    //        Label1.Text = "盘点失败！";
    //    }
    //}

}
